;
(function (call) {
    var CMAREPShhly = {
        mapId: "tjMap",
        selectedPro: [],
        playList: null,
        latestTime: null,
        isFirstGetTime: true,
        router: {
            cfg: "/business/szyb/json/CMAREPShhly.json",
        },
        init: function () {
            var that = this;
            $.getJSON(that.router.cfg, function (cfg) {
                if (cfg) {
                    that.buildContainer(cfg);
                    that.bindEvt();
                }
            })
        },
        // 获取最新起报时间
        getLatestTime: function (pro) {
            var that = this;
            that.isFirstGetTime = false;
            that.latestTime = "2023-07-28 08:00:00";
			$(".product-CMAREPShhly-datepicker #CMAREPShhlyDate").IDatePicker().setVal(that.latestTime);
			that.buildLastTime();
        },
		 // 初始化最近起报时间
        buildLastTime: function (call) {
            var baseDate = $("#CMAREPShhlyDate").IDatePicker().getVal() + ":00";
            var newDate = new Date(baseDate);
            var liArr = [];
            for (var i = 0; i < 4; i++) {
                liArr.push(`<li class="${i == 0 ? 'active' : ''}" data-time="${newDate}">${newDate.format("ddhh")}</li>`);
                newDate.setHours(newDate.getHours() - 12);
            }
            $(".product-CMAREPShhly-datechange ul").empty().append(liArr.reverse().join(""));
			if(call){
				call();
			}
        },
        /**
         * 构建弹出框内容
         * 构建弹出框内容
         */
        buildContainer: function (data) {
            var that = this;
            if (!data) {
                return;
            }
			console.log("buildContainer");
            data.pro_type.forEach(function (item) {
                var $li = $(`<li>${item.name}</li>`);
                $li.data('model', item);
                $li.off().click(function () {
                    if ($(this).hasClass("active")) {
                        return;
                    }
                    that.hideImgContain(); //切换模式后, 清空之前模式数据
                    $(this).addClass("active").siblings().removeClass("active");

                    $('.product-CMAREPShhly-datepicker').empty()
                        .append(`
						<div class="date-panel">
							<input class="DateAgent" id="CMAREPShhlyDate" data-options="${item.dateOptions}" value="">
							<i class="btn-forward"></i>
							<i class="btn-backward"></i>
							<i class="btn-newest"></i>
						</div>`);
                    $.IDate(".product-CMAREPShhly-datepicker .date-panel", {
                        query: function query() {
                            CMAREPShhly.buildLastTime(function () {
                                CMAREPShhly.showTechPdf();
                            });
                        }
                    });
					that.getLatestTime();
					that.buildLastTime();
                    that.buildProContent(data.basic_pro[item.id] || []);
					that.showTechPdf();
					//$(".product-CMAREPShhly-list ul li:first").click();
                })
                $(".product-CMAREPShhly-tab ul").append($li);
            })
            $(".product-CMAREPShhly-tab").hide();
			$(".product-CMAREPShhly-tab ul li:first").click();
        },
        /**
         * 构建要素展示
         * data 要素列表
         */
        buildProContent: function (data) {
            var that = this;
            $(".product-CMAREPShhly-list").empty();
            data.forEach(function (item) {
                var $item = $('<div>').addClass('product-item');
                var $title = $('<div>').addClass('product-title');
                $title.data('type', item.group_id);
                $title.html(item.group_name);
                $item.append($title);
                if (item.content) {
                    var $ul = $('<ul>');
					item.content.forEach(function(pro) {
						var $li = $(`<li><span>${pro.ele_name}</span></li>`);
						if (pro.selected)
							$li.addClass("default");
						$li.data('pro', pro);
						if (pro.title)
							$li.attr('title', pro.title);
						if (pro.width)
							$li.css("width", pro.width);
						$ul.append($li);
					})
					$item.append($ul);
                }
                $(".product-CMAREPShhly-list").append($item);
            })
			$(".product-CMAREPShhly-list ul li:first").addClass("active");
        },
		//点击显示pdf技术说明
        showTechPdf: function () {
			var that = this;
			let id = "cmarepshhlyPdfContain";
			let time = new Date($(".product-CMAREPShhly-datepicker #CMAREPShhlyDate").IDatePicker().getVal() + ":00");
			let newTime  = new Date(new Date(time).getTime() - 8*60*60*1000).format('yyyyMMddhh');
			let interval = $(".product-CMAREPShhly-interval li.active").attr("interval");
			let path = "http://10.226.120.112:9996/operation-myl_stamp_"+newTime+"_"+interval+".pdf";
            if (CafeJS.ModalBoxExist(id)) {
                CafeJS.ModalBoxClose(id);
            }
            CafeJS.ModalBox({
                id: id,
                container: 'cafe-busis-container',
                title: "CMA-REPS-hhly",
                width: 1200,
                height: 700,
                m_top: 70,
                m_left: 580,
                hasMask: false,
                hide: false,
                html: "<div id='" + id + "' style='width:100%;height:calc(100% - 10px)'></div>",
                showCall: function (obj) {
                    PDFObject.embed(path, "#" + id);
                },
                closed: function () { 
					$("#modal_product_pic").remove();
                    $(".product-CMAREPShhly-list li").removeClass('active');
				}
            });
        },
        showImgContain: function () {
			var that = this;
			that.hideImgContain();
			//http://10.226.120.112:9995/202309/laf-2023092720.006.png
			let time = new Date($(".product-CMAREPShhly-datepicker #CMAREPShhlyDate").IDatePicker().getVal() + ":00");
			let interval = $(".product-CMAREPShhly-interval li.active").attr("interval");
			let imgSrc = "http://10.226.120.112:9995/"+time.format('yyyyMM')+"/laf-"+time.format('yyyyMMddhh')+"."+interval+".png";
			
		    CafeJS.ModalBox({
                id: 'CMAREPShhly_dialog',
                container: 'cafe-busis-container',
                title: "CMAREPShhlym-LAG",
                width: 830,
                height: 630,
                m_top: 55,
                m_left: 600,
                hide: true,
                //url: that.router.initProductPicPop,
				html:'<div><img src="'+imgSrc+'"></img></div>',
                closed: function () {
                    $("#modal_product_pic").remove();
                    $(".product-CMAREPShhly-list li").removeClass('active');
                }
            });
        },
        hideImgContain: function () {
          $("#modal_CMAREPShhly_dialog").remove();
        },
        /**
         * 绑定事件操作
         */
        bindEvt: function () {
            var that = this;
            // 要素点击事件
            $(".product-CMAREPShhly-list").off('click').on('click', 'li', function () {
                var pro = $(this).data('pro');
				if ($(this).hasClass("active")) {
                    $(this).removeClass("active");
					let id = "cmarepshhlyPdfContain";
					if (CafeJS.ModalBoxExist(id)) {
						CafeJS.ModalBoxClose(id);
					}
                } else {
                    $(this).addClass("active");
                    that.showTechPdf();;
                }
            })
            // 预报时间间隔
            $(".product-CMAREPShhly-interval").delegate('li', 'click', function() {
                $(this).addClass("active").siblings().removeClass("active");
                that.showTechPdf();
            })
            // 最近起报时间
            $(".product-CMAREPShhly-datechange").delegate('li', 'click', function () {
                $(this).addClass("active").siblings().removeClass("active");
                var time = $(this).data('time');

                $("#CMAREPShhlyDate").IDatePicker().setVal(time);

                that.showTechPdf();
            })
            $('.product-CMAREPShhly-datepicker').off('click').on('click', '.btn-forward', function (e) { // 日期向前
                e.stopPropagation();
                $("#CMAREPShhlyDate").IDatePicker().toForward();
                that.buildLastTime(function () {
                    that.showTechPdf();
                });
            }).on('click', '.btn-backward', function (e) { // 日期向后
                e.stopPropagation();
                $("#CMAREPShhlyDate").IDatePicker().toBackward();
                that.buildLastTime(function () {
                    that.showTechPdf();
                });
            }).on('click', ".btn-newest", function (e) { // 最新
                e.stopPropagation();
                $("#CMAREPShhlyDate").IDatePicker().toNewest();
                that.buildLastTime(function () {
                   that.showTechPdf();
                });
            });
            //$(".product-CMAREPShhly-tab li").first().click();
        }
    }
    if (typeof call === 'function') {
        CafeJS.CMAREPShhly = CMAREPShhly;
        call(CMAREPShhly);
    }
    window.CMAREPShhly = CMAREPShhly;
})(function (CMAREPShhly) {
    CafeJS.models(['ajax', 'legentconfig', 'legentpicker', 'timeLine2'], function () {
        CMAREPShhly.init();
    });
});
